package gui.client;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Client;
import vcpClasses.Order;

import javax.swing.JTabbedPane;
import javax.swing.JCheckBox;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;

import control.LocalOpertaionController;
import control.LoginController;
import control.OrderController;
/**
 * this class use to Close Order Screen
 * @author G8
 *
 */
public class CloseOrderScreen extends JPanel implements ActionListener
{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSave;
	public JButton btnCancelOrder;
	private JTextField OrderNumber;
	private JLabel lblOrderNumber;
	private JTextField textField;
	private OrderController orderController;
	private LocalOpertaionController localOpControl;
	private JButton btnPay;
	private JButton btnRemove;
	private String cost;
	/**
	 * Close Order Screen Constructor 
	 */
	public CloseOrderScreen()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize()
	{
		orderController = new OrderController(null);
		localOpControl = new LocalOpertaionController(null);
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Close Order", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnSave = new JButton("Close Order");
		btnSave.setBounds(180, 175, 134, 35);
		btnSave.addActionListener(this);
		add(btnSave);
		
		OrderNumber = new JTextField();
		OrderNumber.setBounds(274, 128, 80, 20);
		add(OrderNumber);
		
		lblOrderNumber = new JLabel("Order Number :");
		lblOrderNumber.setBounds(144, 131, 160, 14);
		add(lblOrderNumber);
		
		btnPay = new JButton("Pay For Parking");
		btnPay.setEnabled(false);
		btnPay.setBounds(180, 221, 134, 35);
		btnPay.addActionListener(this);
		add(btnPay);
		
		btnRemove = new JButton("Remove Vehicle");
		btnRemove.setEnabled(false);
		btnRemove.setBounds(180, 267, 134, 35);
		btnRemove.addActionListener(this);
		add(btnRemove);
	}

	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		Serializable[] temp = null;
		
		if (e.getSource() == btnBack)
		{
				VCP.setScreen(this, VCP.prevScreen);
				VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
				VCP.prevScreen.setLayout(null);
				VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnSave)
		{
			OrderNumber.setBackground(Color.WHITE);
			
			if (FieldValidator.checkNumeric(OrderNumber.getText()))
			{
				Order o = new Order();
				o.setOrderNumber(Integer.parseInt(OrderNumber.getText()));
				SystemTypes orderToClose = new SystemTypes(o,null);
				SystemTypes closeResult = orderController.closeOrder(orderToClose);
				temp = (Serializable[])closeResult.getObj();
				cost = (String) temp [1];
				if (closeResult.getSysMode().equals(SystemMode.OK)){
					JOptionPane.showMessageDialog(null, temp[0], "Success",JOptionPane.INFORMATION_MESSAGE);
					btnPay.setEnabled(true);
					btnSave.setEnabled(false);
				}
				else{
					OrderNumber.setBackground(Color.RED);
					JOptionPane.showMessageDialog(null,temp[0], "Error",JOptionPane.ERROR_MESSAGE);
				}
			}
			else
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnPay){
			Client client = new Client((Client)VCP.currentUser);
			client.setClientBalance(-1*Float.valueOf((String) cost));
			SystemTypes clientToPay = new SystemTypes(client,null);
			SystemTypes balanceResult = orderController.newBalance(clientToPay);
			if (balanceResult.getSysMode().equals(SystemMode.OK)){
				JOptionPane.showMessageDialog(null, balanceResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
				btnRemove.setEnabled(true);
				btnPay.setEnabled(false);
			}
			else
				JOptionPane.showMessageDialog(null,balanceResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnRemove){
			Order order = new Order();
			order.setOrderNumber(Integer.parseInt(OrderNumber.getText()));
			SystemTypes orderToClose = new SystemTypes(order,null);
			SystemTypes closeResult = localOpControl.removeCar(orderToClose);
			if (closeResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null, closeResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null,closeResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);	
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}

	}
}
